//
//  ObjDataResponse.swift
//  AJiTai
//
//  Created by 周博 on 2020/6/1.
//  Copyright © 2020 BO ZHOU. All rights reserved.
//

import HandyJSON

// 以下用的<T: HandyJSON>其实就是BaseModel 或者BaseModel的子类
// 所有response类型的包装类
open class ObjDataResponse<T: HandyJSON>: BaseResponse {
//    var dataObj: T = T()

    var response: [String: Any]?

    convenience init(with response: [String: Any]?) {
        self.init()

        self.response = response
        
        assignDM(dic: response)
    }

    // MARK: - 返回里层data层model
    // 包装object
    // 懒加载:
    lazy var object: T = {
        
        // TODO: warning: 后期可以将DataResponse, listData等一系列的 code, msg等字段删除, 从response中取出"code", 在self中接过来

        if let model = DataResponse<T>.deserialize(from: response) {

            return model.data
        } else {
            return T()
        }
    }()


    // 包装数组
    lazy var objectList: [T] = {

        if let model = ListDataResponse<T>.deserialize(from: response) {

            return model.data
        } else {
            return [T]()
        }
    }()
    
    // 包装string数组
    lazy var stringList: [String] = {
       
        if let stringList = response?["data"] as? [String] {
            return stringList
        } else {
            return [String]()
        }
    }()
    
    // 包装int数组
    lazy var intList: [Int] = {
       
        if let valueList = response?["data"] as? [Int] {
            return valueList
        } else {
            return [Int]()
        }
    }()
    
    // 包装double数组
    lazy var doubleList: [Double] = {
       
        if let valueList = response?["data"] as? [Double] {
            return valueList
        } else {
            return [Double]()
        }
    }()
    

    // 包装string
    lazy var string: String = {
        if let string = response?["data"] as? String {

            return string
        } else {
            return ""
        }
    }()

    // 包装bool
    lazy var bool: Bool = {
        if let bool = response?["data"] as? Bool {

            return bool
        } else {
            return false
        }
    }()

    // 包装Int
    lazy var int: Int = {
        if let int = response?["data"] as? Int {

            return int
        } else {
            return 0
        }
    }()

    // 包装double
    lazy var double: Double = {
        if let double = response?["data"] as? Double {

            return double
        } else {
            return 0
        }
    }()


    //  赋值msg和code
    func assignDM(dic: [String: Any]?) {
        if let msg = response?["msg"] as? String {
            self.msg = msg
        }
        if let code = response?["code"] as? Int {
            self.code = code
        }
    }
}



// study:
/**
 计算属性: 如果使用计算属性, 每次使用object属性都要进行一次json转model的计算, 消耗性能
 使用懒加载属性
 var object: T {

     if let model = DataResponse<T>.deserialize(from: response) {

         assignDM(baseResponse: model)
         return model.data
     } else {
         return T()
     }
 }
 */

/**
 普通存储属性: 类初始化就会立即计算这个代码块, 给object属性赋值, 但是因为response是从外界赋值而来, 并不能在init的时候就拿到, 所以会编译报错
 解决方法: 添加lazy, 使之变成懒加载属性
 var object: T = {

     if let model = DataResponse<T>.deserialize(from: response) {

         assignDM(baseResponse: model)
         return model.data
     } else {
         return T()
     }
 }()
 */
